package com.canbot.u05.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.adapter.ListViewAdapter;
import com.canbot.u05.adapter.ListViewAdapter.OnIClickListener;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.MyDbUtils;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.CenterDialog;
import com.canbot.u05.view.CenterDialogOne;
import com.canbot.u05.view.IButtonClickListerner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自定义声控对话
 *
 * @author hh
 */
public class CVVDialogListviewActivity extends BaseActivityNoTitle {

        private RelativeLayout mLayoutHead;

        private Button mBack;

        private TextView mTextView;

        private ListView mListView;

        private ListViewAdapter mListViewAdapter;

        private Context mContext;

        private CenterDialog mCenterDialog;

        private ImageView imageView;

        private String music_path = "";

        private String editDate = "";

        private int mPosition;

        private int numDate;

        private CenterDialogOne mCenterDialogOne;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mContext = this;

                setContentView(R.layout.cvvd_activity3);
                mListView = (ListView) findViewById(R.id.lstv);
                initView();
        }

        @Override
        protected void onResume() {
                super.onResume();
                mListViewAdapter.notifyDataSetChanged();
        }

        private void initView() {
                mLayoutHead = (RelativeLayout) findViewById(R.id.head_language);
                mBack = (Button) mLayoutHead.findViewById(R.id.btn_head_back);
                mTextView = (TextView) mLayoutHead.findViewById(R.id.tv_head);
                imageView = (ImageView) findViewById(R.id.iv_shownoadd2);
                imageView.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                if (((MainApplication) getApplication()).mCustomDialogues
                                        .size() >= 100) {
                                        mCenterDialogOne.show();
                                }
                                else {
                                        Intent mIntent = new Intent(CVVDialogListviewActivity.this,
                                                CVVDialogEditActivity.class);
                                        startActivity(mIntent);
                                }
                        }
                });
                mBack.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {

                                List<String> questionList=new ArrayList<>();
                                for(int i=0;i<((MainApplication) getApplication()).mCustomDialogues.size();i++){
                                        questionList.add(((MainApplication) getApplication()).mCustomDialogues.get(i).getQuestion());
                                }
                                MsgSendUtils.sendStringMsgToPad(MsgType.DELETE_ALL_QUESTION, JSON.toJSONString(questionList));
                                Logger.e("save"+"questionList.toString() adddd"+questionList.toString());

                                finish();

                        }
                });
                mTextView.setText("自定义声控对话");
                mListViewAdapter = new ListViewAdapter(mContext,
                        ((MainApplication) getApplication()).mCustomDialogues);
                mListView.setAdapter(mListViewAdapter);
                mCenterDialogOne = new CenterDialogOne(mContext, R.style.centerdialog,
                        "声控对话数量已达上限", new IButtonClickListerner() {

                        @Override
                        public void doBtnRight() {

                        }

                        @Override
                        public void doBtnLeft() {

                        }
                });
                mCenterDialog = new CenterDialog(mContext, R.style.centerdialog,
                        "确定要删除选中的音频吗？", new IButtonClickListerner() {

                        @Override
                        public void doBtnRight() {
                                // 发送数据到优友本机

                                MsgSendUtils.sendStringMsgToPad(
                                        MsgType.DELETE_DIALOGUE,((MainApplication) getApplication()).mCustomDialogues.get(mPosition).getQuestion());
                                Logger.e("save"+"Question"+((MainApplication) getApplication()).mCustomDialogues.get(mPosition).getQuestion());


                                //删除本地数据
                                MyDbUtils.delete(CVVDialogListviewActivity.this,
                                        ((MainApplication) getApplication()).mCustomDialogues
                                                .get(mPosition).getQuestion(),
                                        ((MainApplication) getApplication()).mCustomDialogues
                                                .get(mPosition).getAnswer());
                                ((MainApplication) getApplication()).mCustomDialogues
                                        .remove(mPosition);
                                mListViewAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void doBtnLeft() {
                                // TODO Auto-generated method stub

                        }
                });
                mListViewAdapter.setOnClickListener(new OnIClickListener() {

                                                            @Override
                                                            public void onItemimgOKClick(int position, View view) {

                                                            }

                                                            @Override
                                                            public void onItemdeleteClick(int postion, View view) {
                                                                    mPosition = postion;
                                                                    mCenterDialog.show();

                                                            }

                                                            @Override
                                                            public void onItemAnimClick(int postion, View view) {
                                                                    int type = ((MainApplication) getApplication()).mCustomDialogues
                                                                            .get(postion).getType();
                                                                    String content = ((MainApplication) getApplication()).mCustomDialogues
                                                                            .get(postion).getAnswer();
                                                                    if (ConstUtils.VOICE_TYPE_FILE == type) { // 文件
//                                                                            mSound.playSound(content, null);

                                                                    }
                                                                    else if (ConstUtils.VOICE_TYPE_TTS == type) {// TTS
//                                                                            mSound.playTTs(content);
                                                                            String url = IpMsgConst.HTTP_URL + "guideMapAction";
                                                                            HashMap<String, Object> hashMap = new HashMap<>();
                                                                            hashMap.put("type", 0);
                                                                            hashMap.put("data", content);
                                                                            OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                                                                                    @Override
                                                                                    public void onSuccess(String result) {
                                                                                            Log.e("TTS2Voice", result);
                                                                                    }

                                                                                    @Override
                                                                                    public void onFailure() {
                                                                                            ToastTools.newInstance(mContext).showText(getString(R.string.network_error));
                                                                                    }
                                                                            });

                                                                    }

                                                            }
                                                    }

                );

        }

}
